/**
 * 定义报警故障说明模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.WasteWater.Alarm.alarmDescriptionManager', ['itfreer','waterCommon']);
	
	module.controller('alarmDescriptionManagerController',function($rootScope,$scope,$itfreerHttp,$waterDataCommon){
		/**
		 * 故障数据
		 */
		$scope.descriptionList;
		/**
		 * 查询条件
		 */
		$scope.queryObject={};
		/**
		 * 站点名称
		 */
		$scope.STATION_NAME="";
		/**
		 * 全选
		 */
		$scope.allStationSelected=false;
		/**
		 *新增数据
		 */
		$scope.dataCreate={};
		/**
		 * 站点信息
		 */
		$scope.stationData={};
		//监听左侧列表
	    $scope.$on('dataPramChanged', function(event, data) {
			$scope.queryObject.STATION_ID = data.STATION_ID;
			$scope.STATION_NAME=data.STATION_NAME;
			$scope.dataCreate.STATION_ID=data.STATION_ID;
			$scope.stationData=data;
			$scope.getList();
	    });
		/**
		 * 初始化日期
		 */
		$scope.initQuery = function(){
			var nowDate = new Date();
			var month="";
			var day="";
			var curMonthDays = new Date(nowDate.getFullYear(), (nowDate.getMonth()), 0).getDate();//获取当月有多少天
			if(nowDate.getDate()-1==0){
				month=nowDate.getMonth();
				day=curMonthDays;
			}else{
				month=nowDate.getMonth()+1;
				day=nowDate.getDate();
			}
			$scope.queryObject.start=nowDate.getFullYear()+"-"+month+"-"+day;
			$scope.queryObject.end=nowDate.getFullYear()+"-"+month+"-"+day;
		}
		/**
		 * 选择站点
		 */
		$scope.changStation=function(str){
			$scope.allStationSelected=str;
			if(str==false){
				 $scope.queryObject.STATION_ID=$scope.stationData.STATION_ID
			}
			else{
				 $scope.queryObject.STATION_ID="";
			}
		}
		/**
		 * 查询数据
		 */
       $scope.getList=function(){
    	   if($scope.allStationSelected){
    		   $scope.queryObject.STATION_ID=""; 
    	   }
    	   else{
    		   $scope.queryObject.STATION_ID=$scope.stationData.STATION_ID;
    	   }
    	   $scope.queryObject.format="json";
    	   $waterDataCommon.getPageRows("/Exception/getAlarmDescriptionList.do","HttpDescriptionList",$scope.queryObject,100000,1,function(d){
    		   $scope.descriptionList=d;
			});
       };
       /**
        * 导出
        */
       $scope.exportList=function(){
    	   if($scope.allStationSelected){
    		   $scope.queryObject.STATION_ID=""; 
    	   }
    	   else{
    		   $scope.queryObject.STATION_ID= $scope.stationData.STATION_ID ;
    	   }
    	   $scope.queryObject.format="excel";
			var queryString = $waterDataCommon.jsonTostring($scope.queryObject);
			window.open(getBasePath()+"/Exception/getAlarmDescriptionList.do?"+queryString);
       };
       /**
        * 新建数据
        */
       $scope.onNewList=function(){
    	   $scope.dataCreate={};
    	   if($scope.allStationSelected){
    		   $.messager.alert("提示","请选择站点!","info");
    		   return;
    	   }
    	   $scope.dataCreate.STATION_ID=$scope.queryObject.STATION_ID;
    	   $itfreer.openWindow('description');
       };
       /**
        * 保存数据
        */
       $scope.onSave=function(){
    	   if($scope.allStationSelected){
    		   $scope.dataCreate.STATION_ID=""; 
    	   }
    	   else{
    		   $scope.dataCreate.STATION_ID= $scope.stationData.STATION_ID ;
    	   }
    	   if(!$scope.dataCreate.STATION_ID){
    		   $.messager.alert("提示","站点不能为空!","info");
    		   return;
    	   }
    	   if($scope.dataCreate.STATION_ID==""){
    		   $.messager.alert("提示","站点不能为空!","info");
    		   return;
    	   }
    	   $itfreerHttp.save('BD_ALARM_CAUSE',$scope.dataCreate,function(d){
    		    $scope.getList();
				$itfreer.closeWindow('description');
				$.messager.alert("提示","保存成功","info");
		 });
       }
       /**
        * 编辑
        */
       $scope.onEditClick=function(data){
    	   $scope.dataCreate=angular.copy(data);
    	   $itfreer.openWindow('description');
       };
       /**
        * 删除
        */
       $scope.onDeleteClick=function(data){
    	   $.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$itfreerHttp.deleteByKey("BD_ALARM_CAUSE", {GUID:data.GUID},function(){
						$scope.getList();
						$.messager.alert("提示","删除成功！");
					});		
				}
			});
       };
       /**
        * 取消
        */
      $scope.onCancel=function(){
    	  $scope.getList();
    	  $itfreer.closeWindow('description');
       }
       $scope.initQuery();
	});
}(window, angular));